package com.hitqz.robot.camera;

import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.camera.configure.CameraProperties;
import com.hitqz.robot.camera.exception.EquipmentException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 相机工厂类
 *
 * @author kehanjiang
 * @date 2024/08/14
 */
@Component
public class EquipmentFactory {
    final private Map<BrandEnum, IEquipmentService> serviceMap;

    public EquipmentFactory(List<IEquipmentService> cameraServices, CameraProperties properties) {
        serviceMap = cameraServices.stream().collect(Collectors.toMap(IEquipmentService::brand, x -> x));
        for (IEquipmentService service : cameraServices) {
            if (BrandEnum.HK == service.brand() && !StringUtils.hasText(properties.getHk().getLibPath())) {
                throw new EquipmentException("海康相机初始失败，请配置运行库地址：camera.lib-path.hk");
            }
            if (BrandEnum.DH == service.brand() && !StringUtils.hasText(properties.getDh().getLibPath())) {
                throw new EquipmentException("大华相机初始失败，请配置运行库地址：camera.lib-path.dh");
            }
            service.intSdk();//初始化SDK
        }
    }

    public IEquipmentService getServiceByBrand(String brand){
        BrandEnum type = BrandEnum.getByType(brand.toUpperCase());
        return serviceMap.get(type);
    }

}

